package com.kehutong.contact.app;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import com.kehutong.common.DoveClient;
import com.kehutong.common.errcode.Result;
import com.kehutong.contact.entity.Resident;
import com.kehutong.contact.entity.ResidentHouse;
import com.kehutong.contact.service.ResidentService;

/**
 * 住户房产绑定处理
 *
 * @author: liukunlong
 * @date: 2021-04-01 19:09
 */
@MvcAction
@ReqMapping("/kht-bin/crm/resident/house")
public class ResidentHouseAction {
    
	static final Logger logger = LoggerFactory.getLogger(ResidentHouseAction.class);

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private DoveClient doveClient;

    @Inject
    private ResidentService residentService;

    /**
     * 根据条件住户房屋绑定列表
     *
     * @param jsonObject 查询条件
     * @return 住户房屋绑定列表
     * @throws Exception 异常
     */
    @ReqMapping("/list")
    public Object getResidentHouseList(JSONObject jsonObject) throws Exception {
//        JSONArray khtIds=jsonObject.getJSONArray("khtIds");


        Page<ResidentHouse> result = null;
        FindPage<ResidentHouse> find = jdbcSession.findPage(ResidentHouse.class)
                .eq(jsonObject, "id", "residentNo", "communityNo", "houseNo")
                .in(jsonObject, "bindType")
                .eq("deleted", false)
                .like(jsonObject, "communityName", "houseName")
                .between("createTime", jsonObject, "beginTime", "endTime")
                .order("createTime", "desc")
                .page(jsonObject);
        result = find.exe();
        //设置头像和真实姓名
        if (Objects.nonEmpty(result.getList())) {
            result.getList().forEach(residentHouse -> {
                Resident resident = jdbcSession.findById(Resident.class, residentHouse.getResidentNo());
                residentHouse.setAvatar(resident.getAvatar());
                residentHouse.setRealname(resident.getRealname());
                residentHouse.setMobile(resident.getMobile());
            });
        }
        return result;
    }


    @ReqMapping("/get")
    public Object getResidentHouse(ResidentHouse residentHouse) {
        if (Objects.isNull(residentHouse.getId())) {
            Result.error(1, "编号不能为空.");
        }
        //设置头像和真实姓名
        Resident resident = jdbcSession.findById(Resident.class, residentHouse.getResidentNo());
        residentHouse.setAvatar(resident.getAvatar());
        residentHouse.setRealname(resident.getRealname());
        residentHouse.setMobile(resident.getMobile());
        return residentHouse;
    }


    /**
     * 删除住户房屋绑定(支持冥等性,可以重复操作)
     * 1.解绑物业公司房屋绑定
     * 2.删除房屋的住户对应的申请记录
     *
     * @param residentHouse 住户房屋绑定对象
     * @return 操作结果
     * @throws Exception 异常
     */
    @ReqMapping("/delete")
    public Object deleteResidentHouse(ResidentHouse residentHouse) throws Exception {
        if (Objects.isNull(residentHouse.getId())) {
            Result.error(1, "住户会员房屋信息不能为空");
        }

        //解绑物业公司房屋绑定
        residentService.unbindResidentHouse(residentHouse.getResidentNo(),residentHouse.getHouseNo());
        return Result.success();
    }
}
